<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="searchInpt"
        placeholder="ID"
        clearable
        @keyup.enter.native="search()"
        class="search-btn"
      >
        <el-button
          @click="search()"
          icon="el-icon-search"
          slot="append"
          :loading="searchLoading"
        >搜索</el-button>
      </el-input>
      <div class="lz-tips">
        <label>
          审核状态:
        </label>
      </div>
      <el-select
        v-model="reviewStatusValue"
        placeholder="请选择"
        @change="search()"
      >
        <el-option
          v-for="item in reviewStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
    <div class="table-container">
      <Table :table="dataTable">
        <template
          slot-scope="props"
          slot="status"
        >
          <el-button
            type="text"
            size="small"
            @click="view(props.obj.row)"
            v-if="props.obj.row.status == 1"
          >待审核</el-button>
          <span v-if="props.obj.row.status == 2">
            已通过
          </span>
          <span v-if="props.obj.row.status ==9">
            未通过
          </span>
        </template>
      </Table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          v-if="pageshow"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      title="审核图片"
      :visible.sync="viewImgDialog"
      width="40%"
      top="20px"
    >
      <div class="demo-image__lazy">
        <div
          v-for="(pic,index) in picture"
          :key="index"
        >
          <vue-photo-zoom-pro :url="pic"></vue-photo-zoom-pro>
        </div>
      </div>
      <div>
        <h4>审核结果：</h4>
        <el-radio
          v-model="viewImgRadio"
          label="2"
        >审核通过</el-radio>
        <el-radio
          v-model="viewImgRadio"
          label="9"
        >审核不通过</el-radio>
      </div>
      <div v-if="viewImgRadio == '9'">
        <h4>请选择失败原因：</h4>
        <el-radio
          v-model="viewImgErrorRadio"
          label="1"
        >图片不清晰</el-radio>
        <el-radio
          v-model="viewImgErrorRadio"
          label="2"
        >图片不符合要求</el-radio>
      </div>
      <div style="margin-top:10px; text-align:center;">
        <el-button
          size="small"
          type="primary"
          @click="postPregnancyStatus()"
        >提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Table from "@/components/Table";
import { fetchPregnancyList, fetchPregnancyStatus } from "@/api/table";
import format from "date-format";
export default {
  data() {
    return {
      count: 0,
      limit: 20,
      currentPage: 1,
      searchInpt: "",
      searchLoading: false,
      pageshow: true,
      viewImgDialog: false,
      picture: [],
      //审核图片
      viewImgRadio: "1",
      viewImgErrorRadio: "1",
      viewImgData: {
        pregnancyRelayId: ""
      },
      reviewStatus: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "1",
          label: "待审核"
        },
        {
          value: "2",
          label: "已通过"
        },
        {
          value: "9",
          label: "未通过"
        }
      ],
      reviewStatusValue: "",
      dataTable: {
        loading: false,
        border: true,
        hasOperation: false,
        tr: [
          {
            id: "1",
            label: "ID",
            prop: "userId",
            show: true
          },
          {
            id: "2",
            label: "预产期",
            prop: "expectedDate",
            show: true
          },
          {
            id: "3",
            label: "手机号",
            prop: "tel",
            show: true
          },
          {
            id: "4",
            label: "地址",
            prop: "addr",
            show: true
          },
          {
            id: "5",
            label: "收货人",
            prop: "userName",
            show: true
          },
          {
            id: "6",
            label: "状态",
            prop: "status",
            show: "template"
          },
          {
            id: "7",
            label: "审核原因",
            prop: "auditDescription",
            show: true
          }
        ],
        data: [],
        operation: {}
      }
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      this.getPregnancyList();
    },
    //时间格式化
    formatterTime(t) {
      if (t) {
        const newTime = format("yyyy-MM-dd hh:mm:ss", new Date(t)).split(" ");
        let time = "";
        time += newTime[0] + "\n" + newTime[1];
        return time;
      } else {
        return "";
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getPregnancyList();
    },
    search() {
      this.currentPage = 1;
      this.pageshow = false;
      this.getPregnancyList();
      this.$nextTick(() => {
        this.pageshow = true;
      });
    },
    view(data) {
      this.viewImgRadio = "2";
      this.viewImgErrorRadio = "1";
      this.viewImgDialog = true;
      this.viewImgData.pregnancyRelayId = data.id;
      this.picture = data.bcImg.split(",");
    },
    async postPregnancyStatus() {
      let radioTxt;
      if (this.viewImgRadio == "9" && this.viewImgErrorRadio == "1") {
        radioTxt = "图片不清晰";
      } else if (this.viewImgRadio == "9" && this.viewImgErrorRadio == "2") {
        radioTxt = "图片不符合要求";
      } else {
        radioTxt = "";
      }
      await fetchPregnancyStatus({
        pregnancyRelayId: this.viewImgData.pregnancyRelayId,
        auditDescription: radioTxt,
        status: this.viewImgRadio
      });
      this.getPregnancyList();
      this.viewImgDialog = false;
      this.$message({
        type: "success",
        message: "提交成功"
      });
    },
    async getPregnancyList() {
      this.dataTable.loading = true;
      const res = await fetchPregnancyList({
        pageSize: this.limit,
        pageNo: this.currentPage,
        userId: this.searchInpt,
        status: this.reviewStatusValue
      });
      this.dataTable.loading = false;
      this.dataTable.data = [];
      this.count = res.data.total;
      res.data.list.forEach(item => {
        const data = {};
        data.userId = item.userId;
        data.id = item.id;
        data.userName = item.userName;
        data.createTime = this.formatterTime(item.createTime);
        data.updateTime = this.formatterTime(item.updateTime);
        data.expectedDate = this.formatterTime(item.expectedDate);
        data.bcImg = item.bcImg;
        data.addr = item.addr;
        data.tel = item.tel;
        data.auditDescription = item.auditDescription;
        let txtStatus;
        data.status = item.status;
        this.dataTable.data.push(data);
      });
    }
  },
  components: {
    Table
  }
};
</script>

